package com.xiaobao.controller;

import com.xiaobao.pojo.Result;
import com.xiaobao.pojo.TodayPlan;
import com.xiaobao.service.TodayPlanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

//今日计划控制类
@RestController
public class TodayPlanController {
    private static final Logger log = LoggerFactory.getLogger(TodayPlanController.class);
    private final TodayPlanService todayPlanService;

    public TodayPlanController(TodayPlanService todayPlanService) {
        this.todayPlanService = todayPlanService;
    }

    //获取今日的全部计划
    @GetMapping("/getTodayPlan/{userid}")
    public Result<TodayPlan[]> getTodayPlan(@PathVariable String userid) {
        log.info("获取用户{}的今日计划", userid);
        return new Result<>(todayPlanService.getTodayPlan(userid));
    }
    //添加今日计划
    @PostMapping("/addTodayPlan")
    public Result<TodayPlan> addTodayPlan( @RequestBody TodayPlan todayPlan) {
        //不用返回数据
        if (todayPlanService.addTodayPlan(todayPlan)){
            return new Result<>(true,200,"添加成功",null);
        }else {
            return new Result<>(false,400,"添加失败",null);
        }

    }
    //修改今日计划
    @PutMapping("/updateTodayPlan")
    public Result<TodayPlan> updateTodayPlan(@RequestBody TodayPlan todayPlan) {
        //修改的内容
        log.info("修改今日计划{}", todayPlan.toString());
        //不用返回数据
        if (todayPlanService.updateTodayPlan(todayPlan)){
            return new Result<>(true,200,"修改成功",null);
        }else {
            return new Result<>(false,400,"修改失败",null);
        }
    }
    //删除今日计划
    @DeleteMapping("/deleteTodayPlan")
    public Result<TodayPlan> deleteTodayPlan(@RequestBody TodayPlan todayPlan) {
        if (todayPlanService.deleteTodayPlan(todayPlan)){
            return new Result<>(true,200,"删除成功",null);
        }else {
            return new Result<>(false,400,"删除失败",null);
        }
    }
}
